package com.paginasenlanube.taglib;

import java.io.IOException;

import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;




import com.paginasenlanube.model.Image;

public class TagImage extends TagSupport{
	
	 /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String id;
	private String filename;
	private String websiteId;
	private String key;
	private String type;
	private String classname;
	private String title;
	private String text;
	private Set<Image> images;
	private String modalDeleteId;
	private String modalText;


	public int doStartTag() throws JspException {
		  
	  try{	            	
  
		        if(classname != null)
		        	pageContext.getOut().print("<div id='imageContainer_"+id+"' class='"+classname+"'>");
		        	pageContext.getOut().print("<div class='actions upload'>");
		        if(title != null)
		        	pageContext.getOut().print("<h3><i class='gen-enclosed foundicon-tools'></i>"+title+"</h3>");
		        if(text != null)
		        	pageContext.getOut().print("<p class='note'>"+text+"</p>"); 
		        	pageContext.getOut().print("<section class='upload'>");
		        
		  
		        	pageContext.getOut().print("<button class='uploder_button btn btn-default'>SUBIR IMAGEN</button>");
        	           
	                pageContext.getOut().print("<input class='fileupload' type='file' name='files[]' data-url='controller/upload.htm' data-uploaderid='"+id+"'>"+
   							  "<div id='dropzone_"+id+"' class='dropzone'>Drop files here</div>"+
   							  "<div id='error_msj_"+id+"' class='errror_msj'></div>"+
   							  "<div id='progress_"+id+"' class='progress progress_bar'>"+
   							  "<div class='bar' style='width: 0%;'></div>"+
   							  "</div>"+
   							  "<div id='uploaded-files_"+id+"' class='uploaded_file' data-key='"+key+"' data-type='"+type+"'>");
   							 if(filename != null && !filename.isEmpty()){
   				            	pageContext.getOut().print("<div id='actual_img_"+id+"' class='actual_img'><img width='100%'  src='/userimages/"+websiteId+"/"+filename+"' style='display: block; visibility: visible; width: 400px; height: auto;'></div>");
   				             }else{
   				            	pageContext.getOut().print("<div id='actual_img_"+id+"' class='actual_img'></div>");
   				             }
   							    pageContext.getOut().print("</div>");
	            	
	                pageContext.getOut().print("</section>"); 
	                
	               
	                
	                if(type.equals("gallery")){
	                	pageContext.getOut().print("<form id='imageForm_"+id+"' data-formtype='imageForm'>"); 
	                	pageContext.getOut().print("<section id='header_fields'>"); 
	                	pageContext.getOut().print("<input class='form-control' id='img_title' name='title' type='text' placeholder='Titulo' />"); 
	                	pageContext.getOut().print("<textarea class='form-control' id='img_text' name='caption'  placeholder='Texto'></textarea>"); 
	                	pageContext.getOut().print("<input class='form-control' id='img_link' name='url' type='text' placeholder='Link' />"); 
	                	pageContext.getOut().print("<input class='form-control' id='img_id' name='id' type='hidden' placeholder='' />"); 
	                	pageContext.getOut().print("</section>");          
	                	pageContext.getOut().print("<input type='button' class='btn btn-primary sendCrop' data-uploaderid='"+id+"' value='Agregar'>"); 
	                	pageContext.getOut().print("</form>"); 
	                }
	                
			        pageContext.getOut().print("</div>");
			        
			        if(type.equals("gallery")){
			        	pageContext.getOut().print("<table id='gal_table_"+id+"' width='100%' border='0' cellspacing='0' cellpadding='0' class='table table-striped'>");
			        	for(Image item : images){
			        	pageContext.getOut().print("<tr id='img_"+item.getId()+"'>");
			        	pageContext.getOut().print("<td><img class='gallery_image' src='/userimages/"+websiteId+"/"+item.getFileName()+"'></td>");
			        	pageContext.getOut().print("<td class='text'>"+item.getTitle()+"</td>");
			        	pageContext.getOut().print("<td class='icon'><button class='btn info alternative icon image edit' data-uploaderid='"+id+"' data-imgid='"+item.getId()+"' data-title='"+item.getTitle()+"' data-caption='"+item.getCaption()+"' data-url='"+item.getUrl()+"'></button></td>");
			        	pageContext.getOut().print("<td class='icon'><button class='btn danger alternative icon img_gal remove' data-uploaderid='"+id+"' data-imgid='"+item.getId()+"'></button></td>");
			        	pageContext.getOut().print("<td class='icon'><div class='editImage'><span><img class='img_up' src='css/images/arrow-up.png'/><img class='img_down' src='css/images/arrow-down.png'/></span></div></td>");
			        	pageContext.getOut().print("</tr>");
			        	}                  
			        	pageContext.getOut().print("</table>");
			        }
			        
	                pageContext.getOut().print("</div>");
	                
	                if(this.modalDeleteId != null){
					pageContext.getOut().print("<!-- Modal -->"+
							"<div class='modal fade' id='modal"+id+"' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>"+
								"<div class='modal-dialog'>"+
									"<div class='modal-content'>"+
					        
					        		"<div class='modal-body'>"+
					        			"<h4>"+this.modalText+"</h4>"+
					        		"</div>"+
					        		"<div class='modal-footer'>"+
					        			"<button type='button' class='btn btn-default' data-dismiss='modal'>Cancelar</button>"+
					        			"<button type='button' class='btn btn-danger' data-dismiss='modal' id='"+modalDeleteId+"'>Borrar</button>"+
					        		"</div>"+
					        		"</div>"+
					        	"</div>"+
							"</div>");
	                }
	                
	        } catch (IOException e) {
	            throw new JspException ("Error: IOException" + 
				                        e.getMessage());
	        }
	        return SKIP_BODY;
	    }
		
	    public int doEndTag() throws JspException {
	        return EVAL_PAGE;
	    }

	    public void setFilename(String filename) {
	        this.filename = filename;
	    }
	    
		public void setId(String id) {
			this.id = id;
		}
		
		  public String getWebsiteId() {
				return websiteId;
			}

			public void setWebsiteId(String websiteId) {
				this.websiteId = websiteId;
			}

			public String getKey() {
				return key;
			}

			public void setKey(String key) {
				this.key = key;
			}

			public String getType() {
				return type;
			}

			public void setType(String type) {
				this.type = type;
			}

			public String getClassname() {
				return classname;
			}

			public void setClassname(String classname) {
				this.classname = classname;
			}

			public String getTitle() {
				return title;
			}

			public void setTitle(String title) {
				this.title = title;
			}

			public String getText() {
				return text;
			}

			public void setText(String text) {
				this.text = text;
			}
			
			public Set<com.paginasenlanube.model.Image> getImages() {
				return images;
			}

			public void setImages(Set<com.paginasenlanube.model.Image> images) {
				this.images = images;
			}

			public String getModalDeleteId() {
				return modalDeleteId;
			}

			public void setModalDeleteId(String modalDeleteId) {
				this.modalDeleteId = modalDeleteId;
			}

			public String getModalText() {
				return modalText;
			}

			public void setModalText(String modalText) {
				this.modalText = modalText;
			}


}
